package com.example.his.api.mis.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import com.example.his.api.common.PageUtils;
import com.example.his.api.common.R;
import com.example.his.api.db.dao.SystemDao;
import com.example.his.api.db.pojo.FlowRegulationEntiy;
import com.example.his.api.mis.controller.form.flowRegulation.*;
import com.example.his.api.mis.service.FlowRegulationService;
import com.example.his.api.mis.service.SystemService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController("MisFlowRegulationController")
@RequestMapping("/mis/flowRegulation")
public class FlowRegulationController {
  
  @Resource
  private FlowRegulationService flowRegulationService;
  
  @Resource
  private SystemService systemService;
  
  //  查询科室名称列表
  @GetMapping("/searchPlaceList")
  @SaCheckLogin
  public R searchPlaceList() {
    ArrayList <String> list = flowRegulationService.searchPlaceList();
    return R.ok().put("result", list);
  }
  
  
  //  设置调流，true是自动调流，false是手动调流
  @GetMapping("/searchMode")
  @SaCheckLogin
  public R searchMode() {
    String value = systemService.getItemValue("auto_flow_regulation");
    boolean b = Boolean.parseBoolean(value);
    return R.ok().put("result", b);
  }
  
  
  //  分页获取数据
  @PostMapping("/searchByPage")
  @SaCheckPermission(value = { "ROOT", "FLOW_REGULATION:SELECT" }, mode = SaMode.OR)
  public R searchByPage(@RequestBody @Valid SearchFlowRegulationByPageForm form) {
    Map param = BeanUtil.beanToMap(form);
    Integer page = MapUtil.getInt(param, "page");
    Integer length = MapUtil.getInt(param, "length");
    Integer start = (page - 1) * length;
    param.put("start", start);
    PageUtils pageUtils = flowRegulationService.searchByPage(param);
    
    return R.ok().put("result", pageUtils);
  }
  
  @PostMapping("/insert")
  @SaCheckPermission(value = { "ROOT", "FLOW_REGULATION:INSERT" }, mode = SaMode.OR)
  public R insert(@RequestBody @Valid InsertFlowRegulationForm form) {
    FlowRegulationEntiy param = BeanUtil.toBean(form, FlowRegulationEntiy.class);
    boolean bool = flowRegulationService.insert(param);
    
    return R.ok().put("result", bool);
  }
  
  //  根据id查询碉流信息
  @PostMapping("/searchById")
  @SaCheckPermission(value = { "ROOT", "FLOW_REGULATION:SELECT" }, mode = SaMode.OR)
  public R searchById(@RequestBody @Valid SearchRegulationByIdform form) {
    FlowRegulationEntiy map = flowRegulationService.searchById(form.getId());
    return R.ok().put("result", map);
  }
  
  
  //  更新体检调流记录
  @PostMapping("/update")
  @SaCheckPermission(value = { "ROOT", "FLOW_REGULATION:UPDATE" }, mode = SaMode.OR)
  public R update(@RequestBody @Valid UpdateFlowRegulationForm form) {
    Map param = BeanUtil.beanToMap(form);
    boolean bool = flowRegulationService.update(param);
    
    return R.ok().put("result", bool);
  }
  
  //  批量删除
  @PostMapping("/deleteByIds")
  @SaCheckPermission(value = { "ROOT", "FLOW_REGULATION:DELETE" }, mode = SaMode.OR)
  public R deleteByIds(@RequestBody @Valid DeleteByIdsFlowRegulationForm form) {
    int i = flowRegulationService.deleteByIds(form.getIds());
    
    return R.ok().put("result", i);
  }
}
